<html>
<head><meta charset="utf-8"><title>(Ab)using const generics for validation · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/(Ab)using.20const.20generics.20for.20validation.html">(Ab)using const generics for validation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="170814555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%28Ab%29using%20const%20generics%20for%20validation/near/170814555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/(Ab)using.20const.20generics.20for.20validation.html#170814555">(Jul 14 2019 at 02:30)</a>:</h4>
<p>I've been thinking about data validation in the context of webapps a bunch recently. Combined with const generics, we can get some code like this for validating the length of a value:</p>
<div class="codehilite"><pre><span></span><span class="cp">#![feature(const_generics)]</span><span class="w"></span>

<span class="cp">#[derive(Debug)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">LenString</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">MAX</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">(</span><span class="nb">String</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">MAX</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">LenString</span><span class="o">&lt;</span><span class="p">{</span><span class="w"> </span><span class="n">MAX</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">new</span><span class="p">(</span><span class="n">s</span>: <span class="nc">impl</span><span class="w"> </span><span class="nb">Into</span><span class="o">&lt;</span><span class="nb">String</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="n">Self</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">into</span><span class="p">();</span><span class="w"></span>

<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">MAX</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nb">Some</span><span class="p">(</span><span class="n">Self</span><span class="p">(</span><span class="n">s</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nb">None</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">LenString</span>::<span class="o">&lt;</span><span class="mi">5</span><span class="o">&gt;</span>::<span class="n">new</span><span class="p">(</span><span class="s">&quot;hello&quot;</span><span class="p">));</span><span class="w"> </span><span class="c1">// None</span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">LenString</span>::<span class="o">&lt;</span><span class="mi">5</span><span class="o">&gt;</span>::<span class="n">new</span><span class="p">(</span><span class="s">&quot;hell&quot;</span><span class="p">));</span><span class="w">  </span><span class="c1">// Some</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="170814608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%28Ab%29using%20const%20generics%20for%20validation/near/170814608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/(Ab)using.20const.20generics.20for.20validation.html#170814608">(Jul 14 2019 at 02:32)</a>:</h4>
<p>I'm trying to think if there's some way of doing the same for a more complicated validation, such as matching a regex. I'm not seeing anything obvious, but maybe smarter folk than me might have an inkling.</p>



<a name="170814614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%28Ab%29using%20const%20generics%20for%20validation/near/170814614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/(Ab)using.20const.20generics.20for.20validation.html#170814614">(Jul 14 2019 at 02:32)</a>:</h4>
<p>At least, not until <code>Regex::new</code> is <code>const fn</code> ;-)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>